package com.atguigu.ware.wares.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.common.to.es.SkuHasStock;
import com.atguigu.ware.wares.dao.WmsWareSkuDao;
import com.atguigu.ware.wares.vo.reqVo.OrderToWareTo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.ware.wares.entity.WmsWareSkuEntity;
import com.atguigu.ware.wares.service.WmsWareSkuService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 商品库存
 *
 * @author qhw
 * @email qhw@gmail.com
 * @date 2021-08-03 19:16:41
 */
@RestController
@RequestMapping("ware/waresku")
public class WmsWareSkuController {
    @Autowired
    private WmsWareSkuService wmsWareSkuService;

    @Autowired
    private WmsWareSkuDao wmsWareSkuDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("wares:wmswaresku:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wmsWareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("wares:wmswaresku:info")
    public R info(@PathVariable("id") Long id){
		WmsWareSkuEntity wmsWareSku = wmsWareSkuService.getById(id);

        return R.ok().put("wmsWareSku", wmsWareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("wares:wmswaresku:save")
    public R save(@RequestBody WmsWareSkuEntity wmsWareSku){
		wmsWareSkuService.save(wmsWareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("wares:wmswaresku:update")
    public R update(@RequestBody WmsWareSkuEntity wmsWareSku){
		wmsWareSkuService.updateById(wmsWareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("wares:wmswaresku:delete")
    public R delete(@RequestBody Long[] ids){
		wmsWareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 远程调用-根据skuid查询库存
     */
    @PostMapping("/skuId/info")
    public R skuIdInfo(@RequestBody List<Long> skuIds){
       List<SkuHasStock>  skuHasStocks = wmsWareSkuService.skuIdInfo(skuIds);

       return  R.ok().setData(skuHasStocks);

    }


    /**
     * 商品远程调用根据商品id锁定对应的商品
     */
    @PostMapping("/order/lock")
    public R orderLock(@RequestBody List<OrderToWareTo> orderToWareTos){
        Boolean lock = wmsWareSkuService.orderLock(orderToWareTos);

        return  R.ok().setData(lock);

    }

}
